home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / man / cmds.fmt / gawk.man < prev    next >
Encoding:
Text File  |  1990-04-20  |  40.9 KB  |  1,189 lines

  1.  
  2.  
  3.  
  4. GAWK                      User Commands                      GAWK
  5.  
  6.  
  7.  
  8. NNAAMMEE
  9.      gawk - pattern scanning and processing language
  10.  
  11. SSYYNNOOPPSSIISS
  12.      ggaawwkk [ --aa ] [ --ee ] [ --cc ] [ --CC ] [ --VV ] [ --FF_f_s ] [ --vv
  13.      _v_a_r=_v_a_l ] --ff _p_r_o_g_r_a_m-_f_i_l_e [ ---- ] file ...
  14.      ggaawwkk [ --aa ] [ --ee ] [ --cc ] [ --CC ] [ --VV ] [ --FF_f_s ] [ --vv
  15.      _v_a_r=_v_a_l ] [ ---- ] _p_r_o_g_r_a_m-_t_e_x_t file ...
  16.  
  17. DDEESSCCRRIIPPTTIIOONN
  18.      _G_a_w_k is the GNU Project's implementation of the AWK program-
  19.      ming language.  It conforms to the definition and descrip-
  20.      tion of the language in _T_h_e _A_W_K _P_r_o_g_r_a_m_m_i_n_g _L_a_n_g_u_a_g_e, by
  21.      Aho, Kernighan, and Weinberger, with the additional features
  22.      defined in the System V Release 4 version of UNIX _a_w_k, and
  23.      some GNU-specific extensions.
  24.  
  25.      The command line consists of options to _g_a_w_k itself, the AWK
  26.      program text (if not supplied via the --ff option), and values
  27.      to be made available in the AARRGGCC and AARRGGVV pre-defined AWK
  28.      variables.
  29.  
  30.      _G_a_w_k accepts the following options, which should be avail-
  31.      able on any implementation of the AWK language.
  32.  
  33.      --FF_f_s Use _f_s for the input field separator (the value of the
  34.           FFSS predefined variable).
  35.  
  36.      --vv _v_a_r=_v_a_l
  37.           Assign the value _v_a_l, to the variable _v_a_r, before exe-
  38.           cution of the program begins.  Such variable values are
  39.           available to the BBEEGGIINN block of an AWK program.
  40.  
  41.      --ff _p_r_o_g_r_a_m-_f_i_l_e
  42.           Read the AWK program source from the file _p_r_o_g_r_a_m-_f_i_l_e,
  43.           instead of from the first command line argument.  Mul-
  44.           tiple --ff options may be used.
  45.  
  46.      ----   Signal the end of options. This is useful to allow
  47.           further arguments to the AWK program itself to start
  48.           with a ``-''.  This is mainly for consistency with the
  49.           argument parsing convention used by most other System V
  50.           programs.
  51.  
  52.      The following options are specific to the GNU implementa-
  53.      tion.
  54.  
  55.      --aa   Use AWK style regular expressions as described in the
  56.           book.  This is the current default, but may not be when
  57.           the POSIX P1003.2 standard is finalized.  It is orthog-
  58.           onal to --cc.
  59.  
  60.  
  61.  
  62.  
  63. Free Software Foundation August 24 1989                         1
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. GAWK                      User Commands                      GAWK
  71.  
  72.  
  73.  
  74.      --ee   Use _e_g_r_e_p(1) style regular expressions as described in
  75.           POSIX standard.  This may become the default when the
  76.           POSIX P1003.2 standard is finalized.  It is orthogonal
  77.           to --cc.
  78.  
  79.      --cc   Run in _c_o_m_p_a_t_i_b_i_l_i_t_y mode.  In compatibility mode, _g_a_w_k
  80.           behaves identically to UNIX _a_w_k; none of the GNU-
  81.           specific extensions are recognized.
  82.  
  83.      --CC   Print the short version of the GNU copyright informa-
  84.           tion message on the error output.  This option may
  85.           disappear in a future version of _g_a_w_k.
  86.  
  87.      --VV   Print version information for this particular copy of
  88.           _g_a_w_k on the error output.  This is useful mainly for
  89.           knowing if the current copy of _g_a_w_k on your system is
  90.           up to date with respect to whatever the Free Software
  91.           Foundation is distributing.  This option may disappear
  92.           in a future version of _g_a_w_k.
  93.  
  94.      Any other options are flagged as illegal, but are otherwise
  95.      ignored.
  96.  
  97.      An AWK program consists of a sequence of pattern-action
  98.      statements and optional function definitions.
  99.  
  100.           _p_a_t_t_e_r_n   {{ _a_c_t_i_o_n _s_t_a_t_e_m_e_n_t_s }}
  101.           ffuunnccttiioonn _n_a_m_e((_p_a_r_a_m_e_t_e_r _l_i_s_t)) {{ _s_t_a_t_e_m_e_n_t_s }}
  102.  
  103.      _G_a_w_k first reads the program source from the _p_r_o_g_r_a_m-_f_i_l_e(s)
  104.      if specified, or from the first non-option argument on the
  105.      command line.  The --ff option may be used multiple times on
  106.      the command line.  _G_a_w_k will read the program text as if all
  107.      the _p_r_o_g_r_a_m-_f_i_l_es had been concatenated together.  This is
  108.      useful for building libraries of AWK functions, without hav-
  109.      ing to include them in each new AWK program that uses them.
  110.      To use a library function in a file from a program typed in
  111.      on the command line, specify //ddeevv//ttttyy as one of the
  112.      _p_r_o_g_r_a_m-_f_i_l_es, type your program, and end it with a ^^DD
  113.      (control-d).
  114.  
  115.      The environment variable AAWWKKPPAATTHH specifies a search path to
  116.      use when finding source files named with the --ff option.  If
  117.      this variable does not exist, the default path is
  118.      ""..:://uussrr//lliibb//aawwkk:://uussrr//llooccaall//lliibb//aawwkk"".  If a file name given
  119.      to the --ff option contains a ``/'' character, no path search
  120.      is performed.
  121.  
  122.      _G_a_w_k compiles the program into an internal form, executes
  123.      the code in the BBEEGGIINN block(s) (if any), and then proceeds
  124.      to read each file named in the AARRGGVV array.  If there are no
  125.      files named on the command line, _g_a_w_k reads the standard
  126.  
  127.  
  128.  
  129. Free Software Foundation August 24 1989                         2
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. GAWK                      User Commands                      GAWK
  137.  
  138.  
  139.  
  140.      input.
  141.  
  142.      If a ``file'' named on the command line has the form _v_a_r==_v_a_l
  143.      it is treated as a variable assignment. The variable _v_a_r
  144.      will be assigned the value _v_a_l.  This is most useful for
  145.      dynamically assigning values to the variables AWK uses to
  146.      control how input is broken into fields and records. It is
  147.      also useful for controlling state if multiple passes are
  148.      needed over a single data file.
  149.  
  150.      For each line in the input, _g_a_w_k tests to see if it matches
  151.      any _p_a_t_t_e_r_n in the AWK program.  For each pattern that the
  152.      line matches, the associated _a_c_t_i_o_n is executed.
  153.  
  154. VVAARRIIAABBLLEESS AANNDD FFIIEELLDDSS
  155.      AWK variables are dynamic; they come into existence when
  156.      they are first used. Their values are either floating-point
  157.      numbers or strings, depending upon how they are used. AWK
  158.      also has one dimension arrays; multiply dimensioned arrays
  159.      may be simulated.  There are several pre-defined variables
  160.      that AWK sets as a program runs; these will be described as
  161.      needed and summarized below.
  162.  
  163.      FFiieellddss
  164.  
  165.      As each input line is read, _g_a_w_k splits the line into
  166.      _f_i_e_l_d_s, using the value of the FFSS variable as the field
  167.      separator.  If FFSS is a single character, fields are
  168.      separated by that character.  Otherwise, FFSS is expected to
  169.      be a full regular expression.  In the special case that FFSS
  170.      is a single blank, fields are separated by runs of blanks
  171.      and/or tabs.  Note that the value of IIGGNNOORREECCAASSEE (see below)
  172.      will also affect how fields are split when FFSS is a regular
  173.      expression.
  174.  
  175.      Each field in the input line may be referenced by its posi-
  176.      tion, $$11, $$22, and so on.  $$00 is the whole line. The value of
  177.      a field may be assigned to as well.  Fields need not be
  178.      referenced by constants:
  179.  
  180.           nn == 55
  181.           pprriinntt $$nn
  182.  
  183.      prints the fifth field in the input line.  The variable NNFF
  184.      is set to the total number of fields in the input line.
  185.  
  186.      References to non-existent fields (i.e. fields after $$NNFF),
  187.      produce the null-string. However, assigning to a non-
  188.      existent field (e.g., $$((NNFF++22)) == 55) will increase the value
  189.      of NNFF, create any intervening fields with the null string as
  190.      their value, and cause the value of $$00 to be recomputed,
  191.  
  192.  
  193.  
  194.  
  195. Free Software Foundation August 24 1989                         3
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. GAWK                      User Commands                      GAWK
  203.  
  204.  
  205.  
  206.      with the fields being separated by the value of OOFFSS.
  207.  
  208.      BBuuiilltt--iinn VVaarriiaabblleess
  209.  
  210.      AWK's built-in variables are:
  211.  
  212.           AARRGGCC the number of command line arguments (does not
  213.                include options to _g_a_w_k, or the program source).
  214.  
  215.           AARRGGVV array of command line arguments. The array is
  216.                indexed from 0 to AARRGGCC - 1.  Dynamically changing
  217.                the contents of AARRGGVV can control the files used
  218.                for data.
  219.  
  220.           EENNVVIIRROONN
  221.                An array containing the values of the current
  222.                environment.  The array is indexed by the environ-
  223.                ment variables, each element being the value of
  224.                that variable (e.g., EENNVVIIRROONN[[""HHOOMMEE""]] might be
  225.                //uu//aarrnnoolldd).  Changing this array does not affect
  226.                the environment seen by programs which _g_a_w_k spawns
  227.                via redirection or the ssyysstteemm function.  (This may
  228.                change in a future version of _g_a_w_k.)
  229.  
  230.           FFIILLEENNAAMMEE
  231.                the name of the current input file.  If no files
  232.                are specified on the command line, the value of
  233.                FFIILLEENNAAMMEE is ``-''.
  234.  
  235.           FFNNRR  the input record number in the current input file.
  236.  
  237.           FFSS   the input field separator, a blank by default.
  238.  
  239.           IIGGNNOORREECCAASSEE
  240.                Controls the case-sensitivity of all regular
  241.                expression operations. If IIGGNNOORREECCAASSEE has a non-
  242.                zero value, then pattern matching in rules, field
  243.                splitting with FFSS, regular expression matching
  244.                with ~~ and !!~~, and the ggssuubb(()), iinnddeexx(()), mmaattcchh(()),
  245.                sspplliitt(()), and ssuubb(()) pre-defined functions will all
  246.                ignore case when doing regular expression opera-
  247.                tions.  Thus, if IIGGNNOORREECCAASSEE is not equal to zero,
  248.                //aaBB// matches all of the strings ""aabb"", ""aaBB"", ""AAbb"",
  249.                and ""AABB"".  As with all AWK variables, the initial
  250.                value of IIGGNNOORREECCAASSEE is zero, so all regular
  251.                expression operations are normally case-sensitive.
  252.  
  253.           NNFF   the number of fields in the current input record.
  254.  
  255.           NNRR   the total number of input records seen so far.
  256.  
  257.           OOFFMMTT the output format for numbers, %%..66gg by default.
  258.  
  259.  
  260.  
  261. Free Software Foundation August 24 1989                         4
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. GAWK                      User Commands                      GAWK
  269.  
  270.  
  271.  
  272.           OOFFSS  the output field separator, a blank by default.
  273.  
  274.           OORRSS  the output record separator, by default a newline.
  275.  
  276.           RRSS   the input record separator, by default a newline.
  277.                RRSS is exceptional in that only the first character
  278.                of its string value is used for separating
  279.                records. If RRSS is set to the null string, then
  280.                records are separated by blank lines.  When RRSS is
  281.                set to the null string, then the newline character
  282.                always acts as a field separator, in addition to
  283.                whatever value FFSS may have.
  284.  
  285.           RRSSTTAARRTT
  286.                the index of the first character matched by
  287.                mmaattcchh(()); 0 if no match.
  288.  
  289.           RRLLEENNGGTTHH
  290.                the length of the string matched by mmaattcchh(()); -1 if
  291.                no match.
  292.  
  293.           SSUUBBSSEEPP
  294.                the character used to separate multiple subscripts
  295.                in array elements, by default ""\\003344"".
  296.  
  297.      AArrrraayyss
  298.  
  299.      Arrays are subscripted with an expression between square
  300.      brackets ([[ and ]]).  If the expression is an expression list
  301.      (_e_x_p_r, _e_x_p_r ...) then the array subscript is a string con-
  302.      sisting of the concatenation of the (string) value of each
  303.      expression, separated by the value of the SSUUBBSSEEPP variable.
  304.      This facility is used to simulate multiply dimensioned
  305.      arrays. For example:
  306.  
  307.           ii == ""AA"" ;; jj == ""BB"" ;; kk == ""CC""
  308.           xx[[ii,, jj,, kk]] == ""hheelllloo,, wwoorrlldd\\nn""
  309.  
  310.      assigns the string ""hheelllloo,, wwoorrlldd\\nn"" to the element of the
  311.      array xx which is indexed by the string ""AA\\003344BB\\003344CC"". All
  312.      arrays in AWK are associative, i.e. indexed by string
  313.      values.
  314.  
  315.      The special operator iinn may be used in an iiff or wwhhiillee state-
  316.      ment to see if an array has an index consisting of a partic-
  317.      ular value.
  318.  
  319.           iiff ((vvaall iinn aarrrraayy))
  320.                pprriinntt aarrrraayy[[vvaall]]
  321.  
  322.      If the array has multiple subscripts, use ((ii,, jj)) iinn aarrrraayy.
  323.  
  324.  
  325.  
  326.  
  327. Free Software Foundation August 24 1989                         5
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. GAWK                      User Commands                      GAWK
  335.  
  336.  
  337.  
  338.      The iinn construct may also be used in a ffoorr loop to iterate
  339.      over all the elements of an array.
  340.  
  341.      An element may be deleted from an array using the ddeelleettee
  342.      statement.
  343.  
  344.      VVaarriiaabbllee TTyyppiinngg
  345.  
  346.      Variables and fields may be (floating point) numbers, or
  347.      strings, or both. How the value of a variable is interpreted
  348.      depends upon its context. If used in a numeric expression,
  349.      it will be treated as a number, if used as a string it will
  350.      be treated as a string.
  351.  
  352.      To force a variable to be treated as a number, add 0 to it;
  353.      to force it to be treated as a string, concatenate it with
  354.      the null string.
  355.  
  356.      The AWK language defines comparisons as being done numeri-
  357.      cally if possible, otherwise one or both operands are con-
  358.      verted to strings and a string comparison is performed.
  359.  
  360.      Uninitialized variables have the numeric value 0 and the
  361.      string value "" (the null, or empty, string).
  362.  
  363. PPAATTTTEERRNNSS AANNDD AACCTTIIOONNSS
  364.      AWK is a line oriented language. The pattern comes first,
  365.      and then the action. Action statements are enclosed in {{ and
  366.      }}.  Either the pattern may be missing, or the action may be
  367.      missing, but, of course, not both. If the pattern is miss-
  368.      ing, the action will be executed for every single line of
  369.      input.  A missing action is equivalent to
  370.  
  371.           {{ pprriinntt }}
  372.  
  373.      which prints the entire line.
  374.  
  375.      Comments begin with the ``#'' character, and continue until
  376.      the end of the line.  Blank lines may be used to separate
  377.      statements.  Normally, a statement ends with a newline, how-
  378.      ever, this is not the case for lines ending in a ``,'',
  379.      ``{'', ``?'', ``:'', ``&&'', or ``||''.  Lines ending in ddoo
  380.      or eellssee also have their statements automatically continued
  381.      on the following line.  In other cases, a line can be con-
  382.      tinued by ending it with a ``\'', in which case the newline
  383.      will be ignored.
  384.  
  385.      Multiple statements may be put on one line by separating
  386.      them with a ``;''.  This applies to both the statements
  387.      within the action part of a pattern-action pair (the usual
  388.  
  389.  
  390.  
  391.  
  392.  
  393. Free Software Foundation August 24 1989                         6
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. GAWK                      User Commands                      GAWK
  401.  
  402.  
  403.  
  404.      case), and to the pattern-action statements themselves.
  405.  
  406.      PPaatttteerrnnss
  407.      AWK patterns may be one of the following:
  408.  
  409.           BBEEGGIINN
  410.           EENNDD
  411.           //_r_e_g_u_l_a_r _e_x_p_r_e_s_s_i_o_n//
  412.           _r_e_l_a_t_i_o_n_a_l _e_x_p_r_e_s_s_i_o_n
  413.           _p_a_t_t_e_r_n &&&& _p_a_t_t_e_r_n
  414.           _p_a_t_t_e_r_n |||| _p_a_t_t_e_r_n
  415.           _p_a_t_t_e_r_n ?? _p_a_t_t_e_r_n :: _p_a_t_t_e_r_n
  416.           ((_p_a_t_t_e_r_n))
  417.           !! _p_a_t_t_e_r_n
  418.           _p_a_t_t_e_r_n_1,, _p_a_t_t_e_r_n_2
  419.  
  420.      BBEEGGIINN and EENNDD are two special kinds of patterns which are
  421.      not tested against the input.  The action parts of all BBEEGGIINN
  422.      patterns are merged as if all the statements had been writ-
  423.      ten in a single BBEEGGIINN block. They are executed before any of
  424.      the input is read. Similarly, all the EENNDD blocks are merged,
  425.      and executed when all the input is exhausted (or when an
  426.      eexxiitt statement is executed).  BBEEGGIINN and EENNDD patterns cannot
  427.      be combined with other patterns in pattern expressions.
  428.      BBEEGGIINN and EENNDD patterns cannot have missing action parts.
  429.  
  430.      For //_r_e_g_u_l_a_r _e_x_p_r_e_s_s_i_o_n// patterns, the associated statement
  431.      is executed for each input line that matches the regular
  432.      expression.  Regular expressions are the same as those in
  433.      _e_g_r_e_p(1), and are summarized below.
  434.  
  435.      A _r_e_l_a_t_i_o_n_a_l _e_x_p_r_e_s_s_i_o_n may use any of the operators defined
  436.      below in the section on actions.  These generally test
  437.      whether certain fields match certain regular expressions.
  438.  
  439.      The &&&&, ||||, and !! operators are logical AND, logical OR, and
  440.      logical NOT, respectively, as in C.  They do short-circuit
  441.      evaluation, also as in C, and are used for combining more
  442.      primitive pattern expressions. As in most languages,
  443.      parentheses may be used to change the order of evaluation.
  444.  
  445.      The ??:: operator is like the same operator in C. If the first
  446.      pattern is true then the pattern used for testing is the
  447.      second pattern, otherwise it is the third. Only one of the
  448.      second and third patterns is evaluated.
  449.  
  450.      The _p_a_t_t_e_r_n_1,, _p_a_t_t_e_r_n_2 form of an expression is called a
  451.      range pattern.  It matches all input lines starting with a
  452.      line that matches _p_a_t_t_e_r_n_1, and continuing until a line that
  453.      matches _p_a_t_t_e_r_n_2, inclusive. It does not combine with any
  454.  
  455.  
  456.  
  457.  
  458.  
  459. Free Software Foundation August 24 1989                         7
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466. GAWK                      User Commands                      GAWK
  467.  
  468.  
  469.  
  470.      other sort of pattern expression.
  471.  
  472.      RReegguullaarr EExxpprreessssiioonnss
  473.      Regular expressions are the extended kind found in _e_g_r_e_p.
  474.      They are composed of characters as follows:
  475.  
  476.           _c    matches the non-metacharacter _c.
  477.  
  478.           _\_c   matches the literal character _c.
  479.  
  480.           ..    matches any character except newline.
  481.  
  482.           ^^    matches the beginning of a line or a string.
  483.  
  484.           $$    matches the end of a line or a string.
  485.  
  486.           [[_a_b_c...]]
  487.                character class, matches any of the characters
  488.                _a_b_c....
  489.  
  490.           [[^^_a_b_c...]]
  491.                negated character class, matches any character
  492.                except _a_b_c... and newline.
  493.  
  494.           _r_1||_r_2
  495.                alternation: matches either _r_1 or _r_2.
  496.  
  497.           _r_1_r_2 concatenation: matches _r_1, and then _r_2.
  498.  
  499.           _r++   matches one or more _r's.
  500.  
  501.           _r**   matches zero or more _r's.
  502.  
  503.           _r??   matches zero or one _r's.
  504.  
  505.           ((_r))  grouping: matches _r.
  506.      The escape sequences that are valid in string constants (see
  507.      below) are also legal in regular expressions.
  508.  
  509.      AAccttiioonnss
  510.      Action statements are enclosed in braces, {{ and }}.  Action
  511.      statements consist of the usual assignment, conditional, and
  512.      looping statements found in most languages. The operators,
  513.      control statements, and input/output statements available
  514.      are patterned after those in C.
  515.  
  516.      OOppeerraattoorrss
  517.  
  518.      The operators in AWK, in order of increasing precedence, are
  519.  
  520.           == ++== --== **== //== %%== ^^==
  521.                Assignment. Both absolute assignment ((_v_a_r == _v_a_l_u_e))
  522.  
  523.  
  524.  
  525. Free Software Foundation August 24 1989                         8
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532. GAWK                      User Commands                      GAWK
  533.  
  534.  
  535.  
  536.                and operator-assignment (the other forms) are sup-
  537.                ported.
  538.  
  539.           ??::   The C conditional expression. This has the form
  540.                _e_x_p_r_1 ?? _e_x_p_r_2 :: _e_x_p_r_3. If _e_x_p_r_1 is true, the value
  541.                of the expression is _e_x_p_r_2, otherwise it is _e_x_p_r_3.
  542.                Only one of _e_x_p_r_2 and _e_x_p_r_3 is evaluated.
  543.  
  544.           ||||   logical OR.
  545.  
  546.           &&&&   logical AND.
  547.  
  548.           ~~ !!~~ regular expression match, negated match.
  549.  
  550.           << <<== >> >>== !!== ====
  551.                the regular relational operators.
  552.  
  553.           _b_l_a_n_k
  554.                string concatenation.
  555.  
  556.           ++ --  addition and subtraction.
  557.  
  558.           ** // %%
  559.                multiplication, division, and modulus.
  560.  
  561.           ++ -- !!
  562.                unary plus, unary minus, and logical negation.
  563.  
  564.           ^^    exponentiation (**** may also be used, and ****== for
  565.                the assignment operator).
  566.  
  567.           ++++ ----
  568.                increment and decrement, both prefix and postfix.
  569.  
  570.           $$    field reference.
  571.  
  572.      CCoonnttrrooll SSttaatteemmeennttss
  573.  
  574.      The control statements are as follows:
  575.  
  576.           iiff ((_c_o_n_d_i_t_i_o_n)) _s_t_a_t_e_m_e_n_t [ eellssee _s_t_a_t_e_m_e_n_t ]
  577.           wwhhiillee ((_c_o_n_d_i_t_i_o_n)) _s_t_a_t_e_m_e_n_t
  578.           ddoo _s_t_a_t_e_m_e_n_t wwhhiillee ((_c_o_n_d_i_t_i_o_n))
  579.           ffoorr ((_e_x_p_r_1;; _e_x_p_r_2;; _e_x_p_r_3)) _s_t_a_t_e_m_e_n_t
  580.           ffoorr ((_v_a_r iinn _a_r_r_a_y)) _s_t_a_t_e_m_e_n_t
  581.           bbrreeaakk
  582.           ccoonnttiinnuuee
  583.           ddeelleettee _a_r_r_a_y[[_i_n_d_e_x]]
  584.           eexxiitt [ _e_x_p_r_e_s_s_i_o_n ]
  585.           {{ _s_t_a_t_e_m_e_n_t_s }}
  586.  
  587.  
  588.  
  589.  
  590.  
  591. Free Software Foundation August 24 1989                         9
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598. GAWK                      User Commands                      GAWK
  599.  
  600.  
  601.  
  602.      II//OO SSttaatteemmeennttss
  603.  
  604.      The input/output statements are as follows:
  605.  
  606.           cclloossee((_f_i_l_e_n_a_m_e))
  607.                close file (or pipe, see below).
  608.  
  609.           ggeettlliinnee
  610.                set $$00 from next input record; set NNFF, NNRR, FFNNRR.
  611.  
  612.           ggeettlliinnee <<_f_i_l_e
  613.                set $$00 from next record of _f_i_l_e; set NNFF.
  614.  
  615.           ggeettlliinnee _v_a_r
  616.                set _v_a_r from next input record; set NNFF, FFNNRR.
  617.  
  618.           ggeettlliinnee _v_a_r <<_f_i_l_e
  619.                set _v_a_r from next record of _f_i_l_e.
  620.  
  621.           nneexxtt Stop processing the current input record. The next
  622.                input record is read and processing starts over
  623.                with the first pattern in the AWK program. If the
  624.                end of the input data is reached, the EENNDD
  625.                block(s), if any, are executed.
  626.  
  627.           pprriinntt
  628.                prints the current record.
  629.  
  630.           pprriinntt _e_x_p_r-_l_i_s_t
  631.                prints expressions.
  632.  
  633.           pprriinntt _e_x_p_r-_l_i_s_t >>_f_i_l_e
  634.                prints expressions on _f_i_l_e.
  635.  
  636.           pprriinnttff _f_m_t, _e_x_p_r-_l_i_s_t
  637.                format and print.
  638.  
  639.           pprriinnttff _f_m_t, _e_x_p_r-_l_i_s_t >>_f_i_l_e
  640.                format and print on _f_i_l_e.
  641.  
  642.           ssyysstteemm((_c_m_d-_l_i_n_e))
  643.                execute the command _c_m_d-_l_i_n_e, and return the exit
  644.                status.  (This may not be available on systems
  645.                besides UNIX and GNU.)
  646.  
  647.      Other input/output redirections are also allowed. For pprriinntt
  648.      and pprriinnttff, >>>>_f_i_l_e appends output to the _f_i_l_e, while || _c_o_m_-
  649.      _m_a_n_d writes on a pipe.  In a similar fashion, _c_o_m_m_a_n_d || ggeett--
  650.      lliinnee pipes into ggeettlliinnee.  GGeettlliinnee will return 0 on end of
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657. Free Software Foundation August 24 1989                        10
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664. GAWK                      User Commands                      GAWK
  665.  
  666.  
  667.  
  668.      file, and -1 on an error.
  669.  
  670.      TThhee _p_r_i_n_t_f SSttaatteemmeenntt
  671.  
  672.      The AWK versions of the pprriinnttff and sspprriinnttff (see below) func-
  673.      tions accept the following conversion specification formats:
  674.  
  675.           %%cc   An ASCII character.  If the argument used for %%cc
  676.                is numeric, it is treated as a character and
  677.                printed.  Otherwise, the argument is assumed to be
  678.                a string, and the only first character of that
  679.                string is printed.
  680.  
  681.           %%dd   A decimal number (the integer part).
  682.  
  683.           %%ii   Just like %%dd.
  684.  
  685.           %%ee   A floating point number of the form
  686.                [[--]]dd..ddddddddddddEE[[++--]]dddd.
  687.  
  688.           %%ff   A floating point number of the form [[--]]dddddd..dddddddddddd.
  689.  
  690.           %%gg   Use ee or ff conversion, whichever is shorter, with
  691.                nonsignificant zeros suppressed.
  692.  
  693.           %%oo   An unsigned octal number (again, an integer).
  694.  
  695.           %%ss   A character string.
  696.  
  697.           %%xx   An unsigned hexadecimal number (an integer).
  698.  
  699.           %%XX   Like %%xx, but using AABBCCDDEEFF instead of aabbccddeeff.
  700.  
  701.           %%%%   A single %% character; no argument is converted.
  702.  
  703.      There are optional, additional parameters that may lie
  704.      between the %% and the control letter:
  705.  
  706.           --    The expression should be left-justified within its
  707.                field.
  708.  
  709.           _w_i_d_t_h
  710.                The field should be padded to this width. If the
  711.                number has a leading zero, then the field will be
  712.                padded with zeros.  Otherwise it is padded with
  713.                blanks.
  714.  
  715.           .._p_r_e_c
  716.                A number indicating the maximum width of strings
  717.                or digits to the right of the decimal point.
  718.  
  719.      The dynamic _w_i_d_t_h and _p_r_e_c capabilities of the C library
  720.  
  721.  
  722.  
  723. Free Software Foundation August 24 1989                        11
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730. GAWK                      User Commands                      GAWK
  731.  
  732.  
  733.  
  734.      pprriinnttff routines are not supported.  However, they may be
  735.      simulated by using the AWK concatenation operation to build
  736.      up a format specification dynamically.
  737.  
  738.      SSppeecciiaall FFiillee NNaammeess
  739.  
  740.      When doing I/O redirection from either pprriinntt or pprriinnttff into
  741.      a file, or via ggeettlliinnee from a file, _g_a_w_k recognizes certain
  742.      special filenames internally.  These filenames allow access
  743.      to open file descriptors inherited from _g_a_w_k's parent pro-
  744.      cess (usually the shell).  The filenames are:
  745.  
  746.           //ddeevv//ssttddiinn
  747.                The standard input.
  748.  
  749.           //ddeevv//ssttddoouutt
  750.                The standard output.
  751.  
  752.           //ddeevv//ssttddeerrrr
  753.                The standard error output.
  754.  
  755.           //ddeevv//ffdd//_n
  756.                The file denoted by the open file descriptor _n.
  757.  
  758.      These are particularly useful for error messages. For exam-
  759.      ple:
  760.  
  761.           pprriinntt ""YYoouu bblleeww iitt!!"" >> ""//ddeevv//ssttddeerrrr""
  762.  
  763.      whereas you would otherwise have to use
  764.  
  765.           pprriinntt ""YYoouu bblleeww iitt!!"" || ""ccaatt 11>>&&22""
  766.  
  767.      These file names may also be used on the command line to
  768.      name data files.
  769.  
  770.      NNuummeerriicc FFuunnccttiioonnss
  771.  
  772.      AWK has the following pre-defined arithmetic functions:
  773.  
  774.           aattaann22((_y,, _x))
  775.                returns the arctangent of _y/_x in radians.
  776.  
  777.           ccooss((_e_x_p_r))
  778.                returns the cosine in radians.
  779.  
  780.           eexxpp((_e_x_p_r))
  781.                the exponential function.
  782.  
  783.           iinntt((_e_x_p_r))
  784.                truncates to integer.
  785.  
  786.  
  787.  
  788.  
  789. Free Software Foundation August 24 1989                        12
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796. GAWK                      User Commands                      GAWK
  797.  
  798.  
  799.  
  800.           lloogg((_e_x_p_r))
  801.                the natural logarithm function.
  802.  
  803.           rraanndd(())
  804.                returns a random number between 0 and 1.
  805.  
  806.           ssiinn((_e_x_p_r))
  807.                returns the sine in radians.
  808.  
  809.           ssqqrrtt((_e_x_p_r))
  810.                the square root function.
  811.  
  812.           ssrraanndd((_e_x_p_r))
  813.                use _e_x_p_r as a new seed for the random number gen-
  814.                erator. If no _e_x_p_r is provided, the time of day
  815.                will be used.  The return value is the previous
  816.                seed for the random number generator.
  817.  
  818.      SSttrriinngg FFuunnccttiioonnss
  819.  
  820.      AWK has the following pre-defined string functions:
  821.  
  822.           ggssuubb((_r,, _s,, _t))
  823.                for each substring matching the regular expression
  824.                _r in the string _t, substitute the string _s, and
  825.                return the number of substitutions.  If _t is not
  826.                supplied, use $$00.
  827.  
  828.           iinnddeexx((_s,, _t))
  829.                returns the index of the string _t in the string _s,
  830.                or 0 if _t is not present.
  831.  
  832.           lleennggtthh((_s))
  833.                returns the length of the string _s.
  834.  
  835.           mmaattcchh((_s,, _r))
  836.                returns the position in _s where the regular
  837.                expression _r occurs, or 0 if _r is not present, and
  838.                sets the values of RRSSTTAARRTT and RRLLEENNGGTTHH.
  839.  
  840.           sspplliitt((_s,, _a,, _r))
  841.                splits the string _s into the array _a on the regu-
  842.                lar expression _r, and returns the number of
  843.                fields. If _r is omitted, FFSS is used instead.
  844.  
  845.           sspprriinnttff((_f_m_t,, _e_x_p_r-_l_i_s_t))
  846.                prints _e_x_p_r-_l_i_s_t according to _f_m_t, and returns the
  847.                resulting string.
  848.  
  849.           ssuubb((_r,, _s,, _t))
  850.                this is just like ggssuubb, but only the first match-
  851.                ing substring is replaced.
  852.  
  853.  
  854.  
  855. Free Software Foundation August 24 1989                        13
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862. GAWK                      User Commands                      GAWK
  863.  
  864.  
  865.  
  866.           ssuubbssttrr((_s,, _i,, _n))
  867.                returns the _n-character substring of _s starting at
  868.                _i.  If _n is omitted, the rest of _s is used.
  869.  
  870.           ttoolloowweerr((_s_t_r))
  871.                returns a copy of the string _s_t_r, with all the
  872.                upper-case characters in _s_t_r translated to their
  873.                corresponding lower-case counterparts.  Non-
  874.                alphabetic characters are left unchanged.
  875.  
  876.           ttoouuppppeerr((_s_t_r))
  877.                returns a copy of the string _s_t_r, with all the
  878.                lower-case characters in _s_t_r translated to their
  879.                corresponding upper-case counterparts.  Non-
  880.                alphabetic characters are left unchanged.
  881.  
  882.      SSttrriinngg CCoonnssttaannttss
  883.  
  884.      String constants in AWK are sequences of characters enclosed
  885.      between double quotes (""). Within strings, certain _e_s_c_a_p_e
  886.      _s_e_q_u_e_n_c_e_s are recognized, as in C. These are:
  887.  
  888.           \\\\   A literal backslash.
  889.  
  890.           \\aa   The ``alert'' character; usually the ASCII BEL
  891.                character.
  892.  
  893.           \\bb   backspace.
  894.  
  895.           \\ff   form-feed.
  896.  
  897.           \\nn   new line.
  898.  
  899.           \\rr   carriage return.
  900.  
  901.           \\tt   horizontal tab.
  902.  
  903.           \\vv   vertical tab.
  904.  
  905.           \\xx_h_e_x _d_i_g_i_t_s
  906.                The character represented by the string of hexade-
  907.                cimal digits following the \\xx.  As in ANSI C, all
  908.                following hexadecimal digits are considered part
  909.                of the escape sequence.  (This feature should tell
  910.                us something about language design by committee.)
  911.                E.g., "\x1B" is the ASCII ESC (escape) character.
  912.  
  913.           \\_d_d_d The character represented by the 1-, 2-, or 3-
  914.                digit sequence of octal digits. E.g. "\033" is the
  915.                ASCII ESC (escape) character.
  916.  
  917.           \\_c   The literal character _c.
  918.  
  919.  
  920.  
  921. Free Software Foundation August 24 1989                        14
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928. GAWK                      User Commands                      GAWK
  929.  
  930.  
  931.  
  932.      The escape sequences may also be used inside constant regu-
  933.      lar expressions (e.g., //[[ \\tt\\ff\\nn\\rr\\vv]]// matches whitespace
  934.      characters).
  935.  
  936. FFUUNNCCTTIIOONNSS
  937.      Functions in AWK are defined as follows:
  938.  
  939.           ffuunnccttiioonn _n_a_m_e((_p_a_r_a_m_e_t_e_r _l_i_s_t)) {{ _s_t_a_t_e_m_e_n_t_s }}
  940.  
  941.      Functions are executed when called from within the action
  942.      parts of regular pattern-action statements. Actual parame-
  943.      ters supplied in the function call are used to instantiate
  944.      the formal parameters declared in the function.  Arrays are
  945.      passed by reference, other variables are passed by value.
  946.  
  947.      Since functions were not originally part of the AWK
  948.      language, the provision for local variables is rather
  949.      clumsy: they are declared as extra parameters in the parame-
  950.      ter list. The convention is to separate local variables from
  951.      real parameters by extra spaces in the parameter list. For
  952.      example:
  953.  
  954.           ffuunnccttiioonn  ff((pp,, qq,,     aa,, bb)) {{ ## aa && bb aarree llooccaall
  955.                          .......... }}
  956.  
  957.           //aabbcc//     {{ ...... ;; ff((11,, 22)) ;; ...... }}
  958.  
  959.      The left parenthesis in a function call is required to
  960.      immediately follow the function name, without any interven-
  961.      ing white space.  This is to avoid a syntactic ambiguity
  962.      with the concatenation operator.  This restriction does not
  963.      apply to the built-in functions listed above.
  964.  
  965.      Functions may call each other and may be recursive.  Func-
  966.      tion parameters used as local variables are initialized to
  967.      the null string and the number zero upon function invoca-
  968.      tion.
  969.  
  970.      The word ffuunncc may be used in place of ffuunnccttiioonn.
  971.  
  972. EEXXAAMMPPLLEESS
  973.      Print and sort the login names of all users:
  974.  
  975.           BBEEGGIINN     {{ FFSS == ""::"" }}
  976.                {{ pprriinntt $$11 || ""ssoorrtt"" }}
  977.  
  978.      Count lines in a file:
  979.  
  980.                {{ nnlliinneess++++ }}
  981.           EENNDD  {{ pprriinntt nnlliinneess }}
  982.  
  983.      Precede each line by its number in the file:
  984.  
  985.  
  986.  
  987. Free Software Foundation August 24 1989                        15
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994. GAWK                      User Commands                      GAWK
  995.  
  996.  
  997.  
  998.           {{ pprriinntt FFNNRR,, $$00 }}
  999.  
  1000.      Concatenate and line number (a variation on a theme):
  1001.  
  1002.           {{ pprriinntt NNRR,, $$00 }}
  1003.  
  1004. SSEEEE AALLSSOO
  1005.      _e_g_r_e_p(1)
  1006.  
  1007.      _T_h_e _A_W_K _P_r_o_g_r_a_m_m_i_n_g _L_a_n_g_u_a_g_e, Alfred V. Aho, Brian W. Ker-
  1008.      nighan, Peter J. Weinberger, Addison-Wesley, 1988. ISBN 0-
  1009.      201-07981-X.
  1010.  
  1011.      _T_h_e _G_A_W_K _M_a_n_u_a_l, published by the Free Software Foundation,
  1012.      1989.
  1013.  
  1014. SSYYSSTTEEMM VV RREELLEEAASSEE 44 CCOOMMPPAATTIIBBIILLIITTYY
  1015.      A primary goal for _g_a_w_k is compatibility with the latest
  1016.      version of UNIX _a_w_k.  To this end, _g_a_w_k incorporates the
  1017.      following user visible features which are not described in
  1018.      the AWK book, but are part of _a_w_k in System V Release 4.
  1019.  
  1020.      The --vv option for assigning variables before program execu-
  1021.      tion starts is new.  The book indicates that command line
  1022.      variable assignment happens when _a_w_k would otherwise open
  1023.      the argument as a file, which is after the BBEEGGIINN block is
  1024.      executed.  However, in earlier implementations, when such an
  1025.      assignment appeared before any file names, the assignment
  1026.      would happen _b_e_f_o_r_e the BBEEGGIINN block was run.  Applications
  1027.      came to depend on this ``feature.'' When _a_w_k was changed to
  1028.      match its documentation, this option was added to accomodate
  1029.      applications that depended upon the old behaviour.
  1030.  
  1031.      When processing arguments, _g_a_w_k uses the special option
  1032.      ``----'' to signal the end of arguments, and warns about, but
  1033.      otherwise ignores, undefined options.
  1034.  
  1035.      The AWK book does not define the return value of ssrraanndd(()).
  1036.      The System V Release 4 version of UNIX _a_w_k has it return the
  1037.      seed it was using, to allow keeping track of random number
  1038.      sequences. Therefore ssrraanndd(()) in _g_a_w_k also returns its
  1039.      current seed.
  1040.  
  1041.      Other new features are: The use of multiple --ff options; the
  1042.      EENNVVIIRROONN array; the \\aa, and \\vv, \\xx escape sequences; the
  1043.      ttoolloowweerr and ttoouuppppeerr built-in functions; and the ANSI C
  1044.      conversion specifications in pprriinnttff.
  1045.  
  1046. GGNNUU EEXXTTEENNSSIIOONNSS
  1047.      _G_a_w_k has some extensions to System V _a_w_k.  They are
  1048.      described in this section.  All the extensions described
  1049.      here can be disabled by compiling _g_a_w_k with --DDSSTTRRIICCTT, or by
  1050.  
  1051.  
  1052.  
  1053. Free Software Foundation August 24 1989                        16
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060. GAWK                      User Commands                      GAWK
  1061.  
  1062.  
  1063.  
  1064.      invoking _g_a_w_k with the --cc option.  If the underlying operat-
  1065.      ing system supports the //ddeevv//ffdd directory and corresponding
  1066.      files, then _g_a_w_k can be compiled with --DDNNOO__DDEEVV__FFDD to disable
  1067.      the special filename processing.
  1068.  
  1069.      The following features of _g_a_w_k are not available in System V
  1070.      _a_w_k.
  1071.  
  1072.           o+    The special file names available for I/O redirec-
  1073.                tion are not recognized.
  1074.  
  1075.           o+    The IIGGNNOORREECCAASSEE variable and its side-effects are
  1076.                not available.
  1077.  
  1078.           o+    No path search is performed for files named via
  1079.                the --ff option.  Therefore the AAWWKKPPAATTHH environment
  1080.                variable is not special.
  1081.  
  1082.           o+    The --aa, --ee, --cc, --CC, and --VV command line options.
  1083.  
  1084.      The AWK book does not define the return value of the cclloossee
  1085.      function.  _G_a_w_k's cclloossee returns the value from _f_c_l_o_s_e(3), or
  1086.      _p_c_l_o_s_e(3), when closing a file or pipe, respectively.
  1087.  
  1088.      When _g_a_w_k is invoked with the --cc option, if the _f_s argument
  1089.      to the --FF option is ``t'', then FFSS will be set to the tab
  1090.      character.  Since this is a rather ugly special case, it is
  1091.      not the default behavior.
  1092.  
  1093. BBUUGGSS
  1094.      The --FF option is not necessary given the command line vari-
  1095.      able assignment feature; it remains only for backwards com-
  1096.      patibility.
  1097.  
  1098.      There are now too many options.  Fortunately, most of them
  1099.      are rarely needed.
  1100.  
  1101. AAUUTTHHOORRSS
  1102.      The original version of UNIX _a_w_k was designed and imple-
  1103.      mented by Alfred Aho, Peter Weinberger, and Brian Kernighan
  1104.      of AT&T Bell Labs. Brian Kernighan continues to maintain and
  1105.      enhance it.
  1106.  
  1107.      Paul Rubin and Jay Fenlason, of the Free Software Founda-
  1108.      tion, wrote _g_a_w_k, to be compatible with the original version
  1109.      of _a_w_k distributed in Seventh Edition UNIX.  John Woods con-
  1110.      tributed a number of bug fixes.  David Trueman of Dalhousie
  1111.      University, with contributions from Arnold Robbins at Emory
  1112.      University, made _g_a_w_k compatible with the new version of
  1113.      UNIX _a_w_k.
  1114.  
  1115.  
  1116.  
  1117.  
  1118.  
  1119. Free Software Foundation August 24 1989                        17
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126. GAWK                      User Commands                      GAWK
  1127.  
  1128.  
  1129.  
  1130. AACCKKNNOOWWLLEEDDGGEEMMEENNTTSS
  1131.      Brian Kernighan of Bell Labs provided valuable assistance
  1132.      during testing and debugging.  We thank him.
  1133.  
  1134.  
  1135.  
  1136.  
  1137.  
  1138.  
  1139.  
  1140.  
  1141.  
  1142.  
  1143.  
  1144.  
  1145.  
  1146.  
  1147.  
  1148.  
  1149.  
  1150.  
  1151.  
  1152.  
  1153.  
  1154.  
  1155.  
  1156.  
  1157.  
  1158.  
  1159.  
  1160.  
  1161.  
  1162.  
  1163.  
  1164.  
  1165.  
  1166.  
  1167.  
  1168.  
  1169.  
  1170.  
  1171.  
  1172.  
  1173.  
  1174.  
  1175.  
  1176.  
  1177.  
  1178.  
  1179.  
  1180.  
  1181.  
  1182.  
  1183.  
  1184.  
  1185. Free Software Foundation August 24 1989                        18
  1186.  
  1187.  
  1188.  
  1189.